import Taro from '@tarojs/taro';

const getSizeByScale = (() => {
  const sys = Taro.getSystemInfoSync();

  return (number, designWidth = 750) => {
    return (number * sys.screenWidth) / designWidth;
  };
})();

function transform2px(v = 0) {
  // 已经转换过了, 就不再转换
  if (/px$/i.test(`${v}`)) {
    return v;
  }

  return getSizeByScale(v) + 'px';
}

function getModuleStyle(styleSettingRender) {
  const keys = ['paddingLeft', 'paddingRight', 'paddingTop', 'paddingBottom'];

  return keys.reduce((r, key) => {
    if (typeof styleSettingRender?.[key] !== 'undefined') {
      r[key] = styleSettingRender[key];
    }

    return r;
  }, {});
}

/**
 *
 *
 * 'paddingLeft'  =>  { paddingLeft: styleSettingRender.paddingLeft }
 *
 * {  paddingLeft: 'paddingLeft2' } => { paddingLeft: styleSettingRender.paddingLeft2 }
 * {  paddingLeft: '0 0 {{paddingLeft}}' } => { paddingLeft: `0 0 ${styleSettingRender.paddingLeft}` }
 * {  flex: '0 0 {{px(width)}}' } => { flex: `0 0 ${transform2px(styleSettingRender.width)}` }
 *
 * ['paddingLeft', 'paddingRight'] => { paddingLeft: styleSettingRender.paddingLeft, paddingRight: styleSettingRender.paddingRight }
 *
 * function fn => fn(styleSettingRender)
 *
 * @param {object} styleSettingRender 配置文件
 * @param  {...any} keys 转换key
 * @returns {object}
 */

function transformOneConfig({ styleSettingRender, content }, ...keys) {
  return keys.reduce((r, item) => {
    if (typeof item === 'string') {
      r[item] = styleSettingRender[item];
    } else if (typeof item === 'function') {
      r = {
        ...r,
        ...item(styleSettingRender, content),
      };
    } else if (Array.isArray(item)) {
      item.forEach((value) => {
        r = {
          ...r,
          ...transformOneConfig({ styleSettingRender, content }, value),
        };
      });
    } else if (typeof item === 'object') {
      r = {
        ...r,
        ...Object.keys(item).reduce((temp, k) => {
          const v = item[k];

          const templateMatches = v.match(/{{(.*?)(?:}})/);
          if (!templateMatches) {
            temp[k] = styleSettingRender[v];

            return temp;
          }

          let param = templateMatches[1].trim();
          let styleValue;

          const pxMatches = param.match(/(transform2px|px)\s*\((.*?)(?:\))/);
          if (pxMatches) {
            styleValue = transform2px(styleSettingRender[pxMatches[2].trim()]);
          } else {
            styleValue = styleSettingRender[param];
          }
          temp[k] = item[k].replace(templateMatches[0], styleValue);

          return temp;
        }, {}),
      };
    }

    return r;
  }, {});
}

function transformConfig(styleSettingRender, o, content) {
  if (!o) {
    return {};
  }

  let temp = {
    // 添加默认的 组件样式
    module: ['paddingLeft', 'paddingRight', 'paddingTop', 'paddingBottom'],
    ...o,
  };

  return Object.keys(temp).reduce((r, key) => {
    r[key] = transformOneConfig({ styleSettingRender, content }, temp[key]);

    return r;
  }, {});
}

const getAdaptiveImgHeight = ((designWidth = 750) => {
  return (imgUrlList, inputWidth) => {
    let v = 0;
    imgUrlList.forEach(({ width, height }) => {
      if (width && height) {
        v = Math.max(height / width, v);
      }
    });

    let outputImgHeight;

    // 图片固定大小
    if (inputWidth > 0) {
      outputImgHeight = v * inputWidth;
    }
    // 图片横向铺满, 中间总的间隔距离
    else {
      inputWidth = inputWidth * -1;
      outputImgHeight = ((designWidth - inputWidth) / imgUrlList.length) * v;
    }

    return getSizeByScale(outputImgHeight);
  };
})();

function splitPrice(price) {
  if (price === undefined || price === null) {
    return [undefined, undefined];
  }

  const [priceLeft, priceRight] = Number(price ?? '')
    .toFixed(2)
    .split('.');

  return [priceLeft, priceRight];
}

export {
  getSizeByScale,
  getModuleStyle,
  transform2px,
  transformOneConfig,
  transformConfig,
  getAdaptiveImgHeight,
  splitPrice,
};
